<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <j-image-upload isMultiple  v-model="model.img" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-input v-model="model.content" placeholder="请输入服务内容"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="需求类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-multi-select-tag type="list_multi" v-model="model.type" dictCode="" placeholder="请选择需求类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginWorkTime">
              <j-date placeholder="请选择开工时间"  v-model="model.beginWorkTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="施工地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workAddress">
              <a-input v-model="model.workAddress" placeholder="请输入施工地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="需要人数(至少一个)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="needPersonNum">
              <a-input-number v-model="model.needPersonNum" placeholder="请输入需要人数(至少一个)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用工天数(至少0.5)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useDays">
              <a-input-number v-model="model.useDays" placeholder="请输入用工天数(至少0.5)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="额外费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="extraAmount">
              <a-input v-model="model.extraAmount" placeholder="请输入额外费用"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input v-model="model.amount" placeholder="请输入服务费"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <a-input v-model="model.userId" placeholder="请输入发布人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="投稿人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="joinId">
              <a-input v-model="model.joinId" placeholder="请输入投稿人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="最后成交价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="okAmount">
              <a-input v-model="model.okAmount" placeholder="请输入最后成交价"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否推荐" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRecommend">
              <j-dict-select-tag type="list" v-model="model.isRecommend" dictCode="yn" placeholder="请选择是否推荐" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'HlServiceBaseForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           title: [
              { required: true, message: '请输入标题!'},
           ],
           img: [
              { required: true, message: '请输入封面!'},
           ],
           content: [
              { required: true, message: '请输入服务内容!'},
           ],
           needPersonNum: [
              { required: true, message: '请输入需要人数(至少一个)!'},
           ],
           useDays: [
              { required: true, message: '请输入用工天数(至少0.5)!'},
           ],
           extraAmount: [
              { required: true, message: '请输入额外费用!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           amount: [
              { required: true, message: '请输入服务费!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           userId: [
              { required: true, message: '请输入发布人!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
           isRecommend: [
              { required: true, message: '请输入是否推荐!'},
           ],
        },
        url: {
          add: "/huole/hlServiceBase/add",
          edit: "/huole/hlServiceBase/edit",
          queryById: "/huole/hlServiceBase/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>